﻿<%@ WebHandler Language="C#" Class="SIPO.Web.AjaxHandler.Loaddata.gettarifbysubsubsectorid" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.Enum;
using SIPO.Web.Support;
using Newtonsoft.Json.Linq;
using System.Text;
namespace SIPO.Web.AjaxHandler.Loaddata
{
    public class gettarifbysubsubsectorid : SIPO.Web.BaseHandler
    {
        recTariffRepo recTarifDal = new recTariffRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        recSubSubSectorRepo recSubSubSectorDal = new recSubSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        recTransactionTypeRepo transTypeDal = new recTransactionTypeRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        recDebtorSectorRepo debtorSectorDal = new recDebtorSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        public override void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);
            recTransactionType transType = new recTransactionType();

            decimal isFormula = 0;
            decimal? value = 0;
            decimal? minvalue = 0;
            decimal? maxvalue = 0;
            decimal? percentage = 0;
            int tarifId = 0;
            int debtorSectorId = 0;
            int debtorId = 0;
            string transtype = string.Empty;
            int registrationEmisionTypeId = 0;
            string registrationlabel = string.Empty;
            int impositionBaseId = 0;
            int subSubSectorId = Convert.ToInt32(context.Request.Params["subSubSectorId"]);
            if (context.Request.Params["debtorId"] != null)
                debtorId = Convert.ToInt32(context.Request.Params["debtorId"]);

            if (context.Request.Params["transtype"] != null)
                transtype = context.Request.Params["transtype"].ToString();
            if (context.Request.Params["registrationEmisionTypeId"] != null)
                registrationEmisionTypeId = Convert.ToInt32(context.Request.Params["registrationEmisionTypeId"]);
            recTariff tarif = new recTariff();
            if (transtype == "perijinan")
            {
                //transType = transTypeDal.GetByLikeTransTypeName("registrasi");
                transType = transTypeDal.GetByTransTypeCode(1);
            }
            else if (transtype == "iuranberkala")
            {
                //transType = transTypeDal.GetByLikeTransTypeName("iuran berkala");
                transType = transTypeDal.GetByTransTypeCode(2);
                recDebtorSector debtorsector = debtorSectorDal.GetById(debtorId, subSubSectorId);
                if (debtorsector != null)
                    debtorSectorId = debtorsector.DebtorSectorID;
            }
            if (transtype == "perijinan" && context.Request.Params["registrationEmisionTypeId"] != null)
                tarif = recTarifDal.GetBySubSubSectorId(subSubSectorId, transType.TransTypeID, registrationEmisionTypeId);
            else
                tarif = recTarifDal.GetBySubSubSectorId(subSubSectorId, transType.TransTypeID);
            recSubSubSector subsubsector = recSubSubSectorDal.GetRegistrationLabelNameById(subSubSectorId);
            if (tarif != null)
            {
                isFormula = tarif.IsFormula == true ? 1 : 0;
                value = tarif.Value == null ? 0 : tarif.Value;
                minvalue = tarif.MinValue == null ? 0 : tarif.MinValue;
                maxvalue = tarif.MaxValue == null ? 0 : tarif.MaxValue;
                percentage = tarif.Percentage == null ? 0 : tarif.Percentage;
                tarifId = tarif.TariffID;
                impositionBaseId = tarif.ImpositionBaseID == null ? 0 : (int)tarif.ImpositionBaseID;
            }
            if (subsubsector != null)
                registrationlabel = subsubsector.recRegistrationLabel.RegistrationLabelName;

            StringBuilder sbProject = new StringBuilder();
            sbProject.Append("{");
            sbProject.AppendFormat("  {0}IsFormula{0}:{0}{1}{0}", "\"", isFormula);
            sbProject.Append(",");
            sbProject.AppendFormat("  {0}Value{0}:{0}{1}{0}", "\"", value);
            sbProject.Append(",");
            sbProject.AppendFormat("  {0}MinValue{0}:{0}{1}{0}", "\"", minvalue);
            sbProject.Append(",");
            sbProject.AppendFormat("  {0}MaxValue{0}:{0}{1}{0}", "\"", maxvalue);
            sbProject.Append(",");
            sbProject.AppendFormat("  {0}Percentage{0}:{0}{1}{0}", "\"", percentage);
            sbProject.Append(",");
            sbProject.AppendFormat("  {0}TariffID{0}:{0}{1}{0}", "\"", tarifId);
            sbProject.Append(",");
            sbProject.AppendFormat("  {0}RegistrationLabelName{0}:{0}{1}{0}", "\"", registrationlabel);
            sbProject.Append(",");
            sbProject.AppendFormat("  {0}DebtorSectorId{0}:{0}{1}{0}", "\"", debtorSectorId);
            sbProject.Append(",");
            sbProject.AppendFormat("  {0}ImpositionBaseId{0}:{0}{1}{0}", "\"", impositionBaseId);
            sbProject.Append("}");

            context.Response.Write(sbProject.ToString());
        }
        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}